package dcutils.cards.standard;

import java.util.ArrayList;
import java.util.Collections;

/**
 * A Deck initializes itself to contain all the values of Suit and Rank combinations.<br/>
 * It can also be initialized with jokers.
 * @author dca
 */
public class Deck extends ArrayList<Card> {
	/** Unique identifier for serialization.<br/> */
	private static final long serialVersionUID = -1020183231459615447L;

	public Deck() {
		this(0);
	} // END constructor

	public Deck(int numJokers) {
		super();

		for(Rank rank : Rank.values()) {
			for(Suit suit : Suit.values()) {
				this.add(new Card(rank, suit));
			} // END loop
		} // END loop

		for(int num = 0 ; num < numJokers ; ++num) {
			this.add(new Card("Joker", "Joker"));
		} // END loop
	} // END constructor

	public Deck shuffle() {
		Collections.shuffle(this);
		return this;
	} // END shuffle

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer(5120);
		buffer.append(this.getClass().getSimpleName());
		buffer.append(" ");
		buffer.append(super.toString());
		return buffer.toString();
	} // END toString

	@Override
	public boolean equals(Object obj) {
		if(null == obj) {
			return false;
		} else if(obj instanceof Deck) {
			Deck deck = (Deck)obj;
			for(Card card : deck) {
				if(!this.contains(card)) {
					return false;
				} // END if
			} // END loop
			return true;
		} else {
			return false;
		} // END if/else
	} // END equals

	@Override
	public int hashCode() {
		int hashCode = 101;
		hashCode = (11 * hashCode) + super.hashCode();
		return hashCode;
	} // END hashCode
} // END class Deck
